<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文章查询功能测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        h1, h2 {
            color: #333;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: inline-block;
            width: 100px;
            font-weight: bold;
        }
        input, select {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            width: 200px;
        }
        button {
            background: #4f46e5;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 10px;
        }
        button:hover {
            background: #3730a3;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 4px;
            background: #f9f9f9;
            white-space: pre-wrap;
        }
        .success {
            border-color: #10b981;
            background: #ecfdf5;
        }
        .error {
            border-color: #ef4444;
            background: #fef2f2;
        }
        .article-item {
            border: 1px solid #ddd;
            padding: 10px;
            margin: 10px 0;
            border-radius: 4px;
            background: white;
        }
        .article-title {
            font-weight: bold;
            color: #4f46e5;
        }
        .article-meta {
            color: #666;
            font-size: 14px;
            margin: 5px 0;
        }
    </style>
</head>
<body>
    <h1>文章查询功能测试</h1>
    
    <div class="container">
        <h2>1. 获取分类列表</h2>
        <button onclick="getCategories()">获取分类列表</button>
        <div id="categoriesResult" class="result"></div>
    </div>

    <div class="container">
        <h2>2. 文章查询测试</h2>
        <div class="form-group">
            <label>文章标题:</label>
            <input type="text" id="title" placeholder="请输入文章标题关键词">
        </div>
        <div class="form-group">
            <label>文章分类:</label>
            <select id="categoryId">
                <option value="">请选择分类</option>
            </select>
        </div>
        <div class="form-group">
            <label>文章状态:</label>
            <select id="status">
                <option value="">请选择状态</option>
                <option value="draft">草稿</option>
                <option value="published">已发布</option>
                <option value="offline">已下线</option>
            </select>
        </div>
        <div class="form-group">
            <label>作者姓名:</label>
            <input type="text" id="authorName" placeholder="请输入作者姓名">
        </div>
        <div class="form-group">
            <label>页码:</label>
            <input type="number" id="page" value="1" min="1">
        </div>
        <div class="form-group">
            <label>页大小:</label>
            <input type="number" id="pageSize" value="20" min="1" max="100">
        </div>
        <button onclick="queryArticles()">查询文章</button>
        <button onclick="resetQuery()">重置查询</button>
        <div id="queryResult" class="result"></div>
    </div>

    <div class="container">
        <h2>3. 快速查询测试</h2>
        <button onclick="queryByCategory()">按分类查询（技术文章）</button>
        <button onclick="queryByStatus()">按状态查询（已发布）</button>
        <button onclick="queryByAuthor()">按作者查询（张三）</button>
        <button onclick="queryCombined()">组合查询（技术+已发布）</button>
    </div>

    <script>
        const API_BASE_URL = 'http://localhost:5095/api';

        // 获取分类列表
        async function getCategories() {
            const resultDiv = document.getElementById('categoriesResult');
            resultDiv.innerHTML = '正在获取分类列表...';
            resultDiv.className = 'result';

            try {
                const response = await fetch(`${API_BASE_URL}/article/categories`);
                const data = await response.json();

                if (data.success) {
                    resultDiv.innerHTML = `获取成功！共 ${data.data.length} 个分类：\n` + 
                        data.data.map(cat => `- ${cat.name} (ID: ${cat.id})`).join('\n');
                    resultDiv.className = 'result success';

                    // 更新分类选择器
                    const categorySelect = document.getElementById('categoryId');
                    categorySelect.innerHTML = '<option value="">请选择分类</option>';
                    data.data.forEach(cat => {
                        const option = document.createElement('option');
                        option.value = cat.id;
                        option.textContent = cat.name;
                        categorySelect.appendChild(option);
                    });
                } else {
                    resultDiv.innerHTML = `获取失败: ${data.message}`;
                    resultDiv.className = 'result error';
                }
            } catch (error) {
                resultDiv.innerHTML = `请求失败: ${error.message}`;
                resultDiv.className = 'result error';
            }
        }

        // 查询文章
        async function queryArticles() {
            const resultDiv = document.getElementById('queryResult');
            resultDiv.innerHTML = '正在查询文章...';
            resultDiv.className = 'result';

            const params = new URLSearchParams();
            
            const title = document.getElementById('title').value.trim();
            const categoryId = document.getElementById('categoryId').value;
            const status = document.getElementById('status').value;
            const authorName = document.getElementById('authorName').value.trim();
            const page = document.getElementById('page').value;
            const pageSize = document.getElementById('pageSize').value;

            if (title) params.append('title', title);
            if (categoryId) params.append('categoryId', categoryId);
            if (status) params.append('status', status);
            if (authorName) params.append('authorName', authorName);
            params.append('page', page);
            params.append('pageSize', pageSize);

            try {
                const response = await fetch(`${API_BASE_URL}/article/list?${params}`);
                const data = await response.json();

                if (data.success) {
                    const articles = data.data.items;
                    const total = data.data.totalCount;
                    
                    let html = `查询成功！共找到 ${total} 条记录，当前页显示 ${articles.length} 条：\n\n`;
                    
                    articles.forEach(article => {
                        html += `<div class="article-item">
                            <div class="article-title">${article.title}</div>
                            <div class="article-meta">
                                分类: ${article.categoryName} | 
                                状态: ${article.status} | 
                                作者: ${article.authorName} | 
                                阅读量: ${article.viewCount} | 
                                创建时间: ${new Date(article.createdAt).toLocaleString()}
                            </div>
                        </div>`;
                    });

                    resultDiv.innerHTML = html;
                    resultDiv.className = 'result success';
                } else {
                    resultDiv.innerHTML = `查询失败: ${data.message}`;
                    resultDiv.className = 'result error';
                }
            } catch (error) {
                resultDiv.innerHTML = `请求失败: ${error.message}`;
                resultDiv.className = 'result error';
            }
        }

        // 重置查询
        function resetQuery() {
            document.getElementById('title').value = '';
            document.getElementById('categoryId').value = '';
            document.getElementById('status').value = '';
            document.getElementById('authorName').value = '';
            document.getElementById('page').value = '1';
            document.getElementById('pageSize').value = '20';
            document.getElementById('queryResult').innerHTML = '';
        }

        // 快速查询测试
        async function queryByCategory() {
            document.getElementById('categoryId').value = 'tech';
            document.getElementById('title').value = '';
            document.getElementById('status').value = '';
            document.getElementById('authorName').value = '';
            await queryArticles();
        }

        async function queryByStatus() {
            document.getElementById('status').value = 'published';
            document.getElementById('title').value = '';
            document.getElementById('categoryId').value = '';
            document.getElementById('authorName').value = '';
            await queryArticles();
        }

        async function queryByAuthor() {
            document.getElementById('authorName').value = '张三';
            document.getElementById('title').value = '';
            document.getElementById('categoryId').value = '';
            document.getElementById('status').value = '';
            await queryArticles();
        }

        async function queryCombined() {
            document.getElementById('categoryId').value = 'tech';
            document.getElementById('status').value = 'published';
            document.getElementById('title').value = '';
            document.getElementById('authorName').value = '';
            await queryArticles();
        }

        // 页面加载时自动获取分类列表
        window.onload = function() {
            getCategories();
        };
    </script>
</body>
</html> 